/*
 * Copyright 2010 - Jeremie Brebec
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.jb.sharedbills.client.form;

import org.jb.sharedbills.client.resources.Resources;
import org.jb.sharedbills.client.stores.EventSaver;
import org.jb.sharedbills.client.stores.EventSaver.SaveStateListener;
import org.jb.sharedbills.client.stores.EventSaver.State;

import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.ui.Image;
import com.google.inject.Inject;

public class SavePanel extends Image implements SaveStateListener {

	@SuppressWarnings("unused")
	private EventSaver eventSaver;

	@Inject
	public SavePanel( EventSaver eventSaver) {
		setResource ( getImageState(eventSaver.getState()) );
		
		this.eventSaver = eventSaver;
		
		eventSaver.addListener(this);
		
		
	}

	@Override
	public void onStateChange(EventSaver.State state) {
		setResource( getImageState(state) );
		setTitle( getStateTitle(state) );
	}

	/**
	 * 
	 * @param state
	 * @return
	 */
	private String getStateTitle(State state) {
		switch( state ) {
		default:
		case COMMIT:
			return "Document synchronisé";
			
		case DIRTY:
		case PARTIAL_COMMIT:
			return "Document modifié";

		case ERROR:
			return "Le document n'a pas pu être correctement sauvegardé";

		case UPDATING:
			return "Synchronisation du document";
			
		case LOCKED:
			return "Impossible de modifier le document";
		}
	}

	/*
	 * 
	 */
	private ImageResource getImageState(State state) {
		switch( state ) {
		default:
		case COMMIT:
			return Resources.INSTANCE.document_ok();
			
		case DIRTY:
		case PARTIAL_COMMIT:
			return Resources.INSTANCE.document_dirty();

		case ERROR:
			return Resources.INSTANCE.document_error();

		case UPDATING:
			return Resources.INSTANCE.document_out();
			
		case LOCKED:
			return Resources.INSTANCE.document_lock();
		}
	}
}
